package com.example.farmer.Setting;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.farmer.R;
import com.example.farmer.Database.UserMessage;
import com.example.farmer.Entity.User;

public class PersonalActivity extends AppCompatActivity {

    private TextView phoneText;
    private EditText nameEdieText;
    private TextView passwordText;
    private TextView perSex;
    private TextView emailText;
    private TextView payPasswordText;

    private Button saveButton;
    private UserMessage userMessage; // 数据库帮助类实例
    private String loggedUserNumber; // 登录用户的号码

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal);

        // 初始化数据库帮助类实例
        userMessage = new UserMessage(this);

        // 初始化UI组件
        nameEdieText = findViewById(R.id.name_edit_text);
        phoneText = findViewById(R.id.phone_text);
        passwordText = findViewById(R.id.password_text);
        perSex = findViewById(R.id.perSex);
        emailText = findViewById(R.id.email_text);
        payPasswordText = findViewById(R.id.paypassword_text);

        saveButton = findViewById(R.id.save_button);

        // 从Intent中获取登录号码，这里假设Intent的额外数据键是"USER_NUMBER"
        loggedUserNumber = getIntent().getStringExtra("USER_NUMBER");

        SharedPreferences sharedPreferences = getSharedPreferences("loginNumber", Context.MODE_PRIVATE);
        String userNumber = sharedPreferences.getString("userNumber" , "") ;
        User user = userMessage.getUserByNumber(userNumber);

        if (user != null){
            nameEdieText.setText("姓名：" + user.getUserName());
            phoneText.setText("电话：" + userNumber);
            passwordText.setText("登录密码：" + user.getPassword());
            perSex.setText("性别：" + user.getSex());
            emailText.setText("邮箱：" + user.getEmail());
            payPasswordText.setText("支付密码：" + user.getPaypassword());

        }else {
            Toast.makeText(this, "未找到学生信息", Toast.LENGTH_SHORT).show();
        }
        // 设置保存按钮的点击事件
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                savePersonalInfo();
            }
        });
    }

    private void savePersonalInfo() {
        // 获取用户输入的数据
        String name = nameEdieText.getText().toString();
        // 支付密码可能需要特殊处理，这里不获取

        // 验证数据有效性
        if (name.isEmpty()) {
            // 显示错误信息
            showToast("请输入完整信息");
            return;
        }

        // 创建User对象，这里假设密码和性别等信息不需要更新
        User user = new User(name);

        // 更新数据到数据库
        boolean isUpdated = userMessage.updateUser(user);
        if (isUpdated) {
            // 显示成功信息
            showToast("信息更新成功");
        } else {
            // 显示错误信息
            showToast("信息更新失败");
        }
    }

    private void showToast(String message) {
        // 显示一个简短的Toast消息
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}